
import { _decorator, Component, Node, find, resources, SpriteFrame, Texture2D, Sprite, } from 'cc';
import { PlayerControl } from './PlayerControl';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = EnemyControl
 * DateTime = Thu Dec 09 2021 11:47:49 GMT+0800 (中国标准时间)
 * Author = huangsz
 * FileBasename = EnemyControl.ts
 * FileBasenameNoExtension = EnemyControl
 * URL = db://assets/scripts/EnemyControl.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('EnemyControl')
export class EnemyControl extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    start() {
        // [3]
    }

    update(deltaTime: number) {
        const pos = this.node.getPosition()
        pos.y -= 200 * deltaTime
        this.node.setPosition(pos)
        if (pos.y <= -400) {
            this.node.destroy()
        }
    }

    die() {
        // resources.load('textures/background', Texture2D, (err, texture) => console.log(err));
        resources.load("images/enemy1_down2/spriteFrame", SpriteFrame, (err, spriteFrame) => {
            this.node.getComponent(Sprite).spriteFrame = spriteFrame
        })

        // 敌机死亡
        setTimeout(() => {
            if (this.node)
                this.node.destroy()
        }, 100)

        // 玩家增加积分
        find('Canvas/Player').getComponent(PlayerControl).score++
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
